<template>
  <div class="system-log-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit($event)"
        >
          添加
        </el-button>
        <!-- <el-button
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete($event)"
        >
          批量删除
        </el-button> -->
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list">
      <el-table-column
        align="center"
        label="字典ID"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="字典名称"
        prop="constant_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="字典代码"
        prop="constant_code"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="字典类型"
        prop="is_system_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="描述"
        prop="constant_desc"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="字典状态"
        prop="constant_status_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="185"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDetail(row)">字典详情</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <dict-detail ref="detail" @fetch-data="fetchData" />
    <dict-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete } from '@/api/dict'
  import dictDetail from './components/detail'
  import dictEdit from './components/edit'

  export default {
    name: 'Dict',
    components: { dictDetail, dictEdit },
    data() {
      return {
        list: [],
        listLoading: true,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleEdit(row) {
        if (row) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDetail(row) {
        this.$refs['detail'].showEdit(row)
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前字典吗', null, async () => {
          const { data } = await doDelete({ const_id: row.id })
          if (data.is_success) {
            this.$baseMessage('删除成功', 'success', 'vab-hey-message-success')
            await this.fetchData()
          }
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList({})
        this.list = data
        this.listLoading = false
      },
    },
  }
</script>
